<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="路由规则 路由规则决定一次 dubbo 服务调用的目标服务器，分为条件路由规则和脚本路由规则，并且支持可扩展。  写入路由规则向注册中心写入路由规则的操作通常由监控中心或治理中心的页面完成 123RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiv">
<meta property="og:type" content="article">
<meta property="og:title" content="kyssion-blog">
<meta property="og:url" content="http://yoursite.com/2018/10/09/dubbo学习和使用（六）dubbo功能（4）-路由/index.html">
<meta property="og:site_name" content="kyssion-blog">
<meta property="og:description" content="路由规则 路由规则决定一次 dubbo 服务调用的目标服务器，分为条件路由规则和脚本路由规则，并且支持可扩展。  写入路由规则向注册中心写入路由规则的操作通常由监控中心或治理中心的页面完成 123RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiv">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-10-09T07:22:37.333Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="kyssion-blog">
<meta name="twitter:description" content="路由规则 路由规则决定一次 dubbo 服务调用的目标服务器，分为条件路由规则和脚本路由规则，并且支持可扩展。  写入路由规则向注册中心写入路由规则的操作通常由监控中心或治理中心的页面完成 123RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiv">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/10/09/dubbo学习和使用（六）dubbo功能（4）-路由/"/>





  <title> | kyssion-blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">kyssion-blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br />
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br />
            
            站点地图
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/dubbo学习和使用（六）dubbo功能（4）-路由/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline"></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="路由规则"><a href="#路由规则" class="headerlink" title="路由规则"></a>路由规则</h2><blockquote>
<p>路由规则决定一次 dubbo 服务调用的目标服务器，分为条件路由规则和脚本路由规则，并且支持可扩展。</p>
</blockquote>
<h3 id="写入路由规则"><a href="#写入路由规则" class="headerlink" title="写入路由规则"></a>写入路由规则</h3><p>向注册中心写入路由规则的操作通常由监控中心或治理中心的页面完成</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();</span><br><span class="line">Registry registry = registryFactory.getRegistry(URL.valueOf(<span class="string">"zookeeper://10.20.153.10:2181"</span>));</span><br><span class="line">registry.register(URL.valueOf(<span class="string">"condition://0.0.0.0/com.foo.BarService?category=routers&amp;dynamic=false&amp;rule="</span> + URL.encode(<span class="string">"host = 10.20.153.10 =&gt; host = 10.20.153.11"</span>) + <span class="string">"));</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>属性解释</p>
</blockquote>
<ul>
<li>condition:// 表示路由规则的类型，支持条件路由规则和脚本路由规则，可扩展，必填。</li>
<li>0.0.0.0 表示对所有 IP 地址生效，如果只想对某个 IP 的生效，请填入具体 IP，必填。</li>
<li>com.foo.BarService 表示只对指定服务生效，必填。</li>
<li>group=foo 对指定服务的指定group生效，不填表示对未配置group的指定服务生效</li>
<li>version=1.0对指定服务的指定version生效，不填表示对未配置version的指定服务生效</li>
<li>category=routers 表示该数据为动态配置类型，必填。</li>
<li>dynamic=false 表示该数据为持久数据，当注册方退出时，数据依然保存在注册中心，必填。</li>
<li>enabled=true 覆盖规则是否生效，可不填，缺省生效。</li>
<li>force=false 当路由结果为空时，是否强制执行，如果不强制执行，路由结果为空的路由规则将自动失效，可不填，缺省为 false。</li>
<li>runtime=false 是否在每次调用时执行路由规则，否则只在提供者地址列表变更时预先执行并缓存结果，调用时直接从缓存中获取路由结果。如果用了参数路由，必须设为 true，需要注意设置会影响调用的性能，可不填，缺省为 false。</li>
<li>priority=1 路由规则的优先级，用于排序，优先级越大越靠前执行，可不填，缺省为 0。</li>
<li>rule=URL.encode(“host = 10.20.153.10 =&gt; host = 10.20.153.11”) 表示路由规则的内容，必填。</li>
</ul>
<h3 id="条件路由规则"><a href="#条件路由规则" class="headerlink" title="条件路由规则"></a>条件路由规则</h3><p>基于条件表达式的路由规则，如：host = 10.20.153.10 =&gt; host = 10.20.153.11</p>
<p>规则解释：</p>
<ul>
<li>=&gt; 之前的为消费者匹配条件，所有参数和消费者的 URL 进行对比，当消费者满足匹配条件时，对该消费者执行后面的过滤规则。</li>
<li>=&gt; 之后为提供者地址列表的过滤条件，所有参数和提供者的 URL 进行对比，消费者最终只拿到过滤后的地址列表。</li>
<li>如果匹配条件为空，表示对所有消费方应用，如：=&gt; host != 10.20.153.11</li>
<li>如果过滤条件为空，表示禁止访问，如：host = 10.20.153.10 =&gt;</li>
</ul>
<h3 id="表达式规则："><a href="#表达式规则：" class="headerlink" title="表达式规则："></a>表达式规则：</h3><blockquote>
<p>参数支持：</p>
</blockquote>
<ul>
<li>服务调用信息，如：method, argument 等，暂不支持参数路由</li>
<li>URL 本身的字段，如：protocol, host, port 等</li>
<li>以及 URL 上的所有参数，如：application, organization 等</li>
</ul>
<blockquote>
<p>条件支持：</p>
</blockquote>
<ul>
<li>等号 = 表示”匹配”，如：host = 10.20.153.10</li>
<li>不等号 != 表示”不匹配”，如：host != 10.20.153.10</li>
</ul>
<blockquote>
<p>值支持：</p>
</blockquote>
<ul>
<li>以逗号 , 分隔多个值，如：host != 10.20.153.10,10.20.153.11</li>
<li>以星号 <em> 结尾，表示通配，如：host != 10.20.</em></li>
<li>以美元符 $ 开头，表示引用消费者参数，如：host = $host</li>
</ul>
<blockquote>
<p>示例：</p>
</blockquote>
<p>排除预发布机：</p>
<ul>
<li>=&gt; host != 172.22.3.91</li>
</ul>
<blockquote>
<p>白名单：</p>
</blockquote>
<ul>
<li>host != 10.20.153.10,10.20.153.11 =&gt;</li>
</ul>
<blockquote>
<p>黑名单：</p>
</blockquote>
<ul>
<li>host = 10.20.153.10,10.20.153.11 =&gt;</li>
</ul>
<blockquote>
<p>服务寄宿在应用上，只暴露一部分的机器，防止整个集群挂掉：</p>
</blockquote>
<ul>
<li>=&gt; host = 172.22.3.1<em>,172.22.3.2</em></li>
</ul>
<blockquote>
<p>为重要应用提供额外的机器：</p>
</blockquote>
<ul>
<li>application != kylin =&gt; host != 172.22.3.95,172.22.3.96</li>
</ul>
<blockquote>
<p>读写分离：</p>
</blockquote>
<ul>
<li>method = find<em>,list</em>,get<em>,is</em> =&gt; host = 172.22.3.94,172.22.3.95,172.22.3.96</li>
<li>method != find<em>,list</em>,get<em>,is</em> =&gt; host = 172.22.3.97,172.22.3.98</li>
</ul>
<blockquote>
<p>前后台分离：</p>
</blockquote>
<ul>
<li>application = bops =&gt; host = 172.22.3.91,172.22.3.92,172.22.3.93</li>
<li>application != bops =&gt; host = 172.22.3.94,172.22.3.95,172.22.3.96</li>
</ul>
<blockquote>
<p>隔离不同机房网段：</p>
</blockquote>
<ul>
<li>host != 172.22.3.<em> =&gt; host != 172.22.3.</em></li>
</ul>
<blockquote>
<p>提供者与消费者部署在同集群内，本机只访问本机的服务：</p>
</blockquote>
<ul>
<li>=&gt; host = $host</li>
</ul>
<h4 id="脚本路由规则"><a href="#脚本路由规则" class="headerlink" title="脚本路由规则"></a>脚本路由规则</h4><p>脚本路由规则支持 JDK 脚本引擎的所有脚本，比如：javascript, jruby, groovy 等，通过 type=javascript 参数设置脚本类型，缺省为 javascript。</p>
<p>“script://0.0.0.0/com.foo.BarService?category=routers&amp;dynamic=false&amp;rule=” + URL.encode(“（function route(invokers) { … } (invokers)）”)</p>
<p>基于脚本引擎的路由规则，如：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">（<span class="function">function <span class="title">route</span><span class="params">(invokers)</span> </span>&#123;</span><br><span class="line">    var result = <span class="keyword">new</span> java.util.ArrayList(invokers.size());</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; invokers.size(); i ++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="string">"10.20.153.10"</span>.equals(invokers.get(i).getUrl().getHost())) &#123;</span><br><span class="line">            result.add(invokers.get(i));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125; (invokers)）; <span class="comment">// 表示立即执行方法</span></span><br></pre></td></tr></table></figure></p>
<ul>
<li>路由规则扩展点：路由扩展 </li>
<li>注意：一个服务只能有一条白名单规则，否则两条规则交叉，就都被筛选掉了</li>
<li>注意：脚本没有沙箱约束，可执行任意代码，存在后门风险 </li>
</ul>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/10/09/dubbo学习和使用（三）dubbo功能（1）/" rel="next" title="">
                <i class="fa fa-chevron-left"></i> 
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/10/09/dubbo学习和使用（二）使用初探和配置方法/" rel="prev" title="">
                 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">kyssion</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">133</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#路由规则"><span class="nav-number">1.</span> <span class="nav-text">路由规则</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#写入路由规则"><span class="nav-number">1.1.</span> <span class="nav-text">写入路由规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#条件路由规则"><span class="nav-number">1.2.</span> <span class="nav-text">条件路由规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#表达式规则："><span class="nav-number">1.3.</span> <span class="nav-text">表达式规则：</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#脚本路由规则"><span class="nav-number">1.3.1.</span> <span class="nav-text">脚本路由规则</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">kyssion</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
